package scau.mis4.chanyulin.utils;

/**
 * Created by cyl on 2017/12/28.
 * 字节工具类
 */
public class ByteUtils {

    /**
     * 将字节数组转换为十六进制字符串
     * @param bytes
     * @return
     */
    public static String toHex(byte[] bytes) {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        StringBuilder hexStr = new StringBuilder("");
        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                hexStr.append(0);
            }
            hexStr.append(hv);
        }
        return hexStr.toString();
    }

    /**
     * 将字节转换为十六进制字符串
     * @param b
     * @return
     */
    public static String toHex(byte b) {
        String hexStr = "";
        int v = b & 0xFF;
        String temp = Integer.toHexString(v);
        if (temp.length() < 2) hexStr = "0";
        hexStr += temp;
        return hexStr;
    }

    /**
     * 字节数组左移
     * @param target
     * @param movCount
     * @return
     */
    public static byte[] movLeft(byte[] target, int movCount) {
        movCount = movCount % target.length;
        if (movCount == 0) return target;
        byte[] result = new byte[target.length];
        byte[] temp = new byte[movCount];
        for (int i = 0; i < movCount; i++)
            temp[i] = target[i];
        for (int j = movCount; j < target.length; j++)
            result[j - movCount] = target[j];
        for (int k = 0; k < movCount; k++)
            result[target.length - movCount + k] = temp[k];
        return result;
    }

    /**
     * 拼合两个字节数组
     * @param bytes1
     * @param bytes2
     * @return
     */
    public static byte[] appendBytes(byte[] bytes1, byte[] bytes2) {
        byte[] result = new byte[bytes1.length + bytes2.length];
        for (int i = 0; i < bytes1.length; i++) result[i] = bytes1[i];
        for (int j = 0; j < bytes2.length; j++) result[bytes1.length + j] = bytes2[j];
        return result;
    }

    /**
     * 判断两个字节数组是否相等
     * @param bytes1
     * @param bytes2
     * @return
     */
    public static boolean isEqual(byte[] bytes1, byte[] bytes2) {
        if (bytes1.length != bytes2.length) return false;
        for (int i = 0; i < bytes1.length; i++) {
            if (bytes1[i] != bytes2[i]) return false;
        }
        return true;
    }

    /**
     * 将字节转为用0和1表示的位数组
     * @param b
     * @return
     */
    public static byte[] toBits(byte b) {
        byte[] array = new byte[8];
        for (int i = 7; i >= 0; i--) {
            array[i] = (byte) (b & 1);
            b = (byte) (b >> 1);
        }
        return array;
    }

    /**
     * 取字节数组的子数组
     * @param bytes
     * @param start
     * @param end
     * @return
     */
    public static byte[] rangeOf(byte[] bytes, int start, int end) {
        if (start > end || start > bytes.length - 1 || end > bytes.length - 1 || start < 0)
            return null;
        byte[] result = new byte[end - start + 1];
        for (int i = 0; i < result.length; i++)
            result[i] = bytes[start + i];
        return result;
    }

}
